﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace Multiprocessing.Core.Units
{
    public sealed class ProcessingUnitFactory : IProcessingUnitFactory
    {
        #region Fields

        private readonly IUnityContainer _container;

        private static readonly Lazy<IProcessingUnitFactory> LazyFactory =
            new Lazy<IProcessingUnitFactory>(() => new ProcessingUnitFactory());

        #endregion

        #region Properties

        public static IProcessingUnitFactory Instance
        {
            get { return LazyFactory.Value; }
        }

        #endregion

        #region Constructors

        private ProcessingUnitFactory()
        {
            _container = new UnityContainer();
            _container.RegisterType<TaskProcessingUnit>();
            _container.RegisterType<WorkerProcessingUnit>();
        }

        #endregion

        #region Methods

        public IProcessingUnit Create<TProcessingUnit>(Action action) where TProcessingUnit : IProcessingUnit
        {
            return _container.Resolve<TProcessingUnit>(new ParameterOverride("action", action));
        }

        public IProcessingUnit Create<TProcessingUnit>() where TProcessingUnit : IProcessingUnit
        {
            return _container.Resolve<TProcessingUnit>();
        }

        #endregion
    }
}
